package dao;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import model.VoyageCTM;

@Stateless
public class VoyageCTMDaoImpl implements IVoyageCTMDao {

	@PersistenceContext(unitName = "putravel")
	EntityManager em;

	@Override
	public VoyageCTM find(String depart, String arrivee, String datedepart) {
		VoyageCTM result;
		Query req = em
				.createQuery("select v from  VoyageCTM v where v.depart.nomagence=:depart and "
						+ "v.arrivee.nomagence=:arrivee and v.datedepart=:date");
		req.setParameter("depart", depart);
		req.setParameter("arrivee", arrivee);
		req.setParameter("date", datedepart);
		try {
			result = (VoyageCTM) req.getSingleResult();
			return result;
		} catch (javax.persistence.NoResultException e) {
			return null;
		}
	}

	@Override
	public void addVoyageCTM(VoyageCTM voyagectm) {
		em.persist(voyagectm);
	}

}
